# Contributor: nibon7 <nibon7@163.com>
# Maintainer: Lindsay Zhou <i@lin.moe>
pkgname=v2ray
pkgver=5.21.0
pkgrel=0
pkgdesc="A platform for building proxies to bypass network restrictions"
url="https://v2fly.org"
# loongarch64: tests failed
arch="all !loongarch64"
license="MIT"
makedepends="go"
subpackages="$pkgname-openrc"
_geosite_ver=20241013063848
_geoip_ver=202410240052
source="$pkgname-core-$pkgver.tar.gz::https://github.com/v2fly/v2ray-core/archive/v$pkgver.tar.gz
	geosite-$_geosite_ver.dat::https://github.com/v2fly/domain-list-community/releases/download/$_geosite_ver/dlc.dat
	geoip-$_geoip_ver.dat::https://github.com/v2fly/geoip/releases/download/$_geoip_ver/geoip.dat
	geoip-only-cn-private-$_geoip_ver.dat::https://github.com/v2fly/geoip/releases/download/$_geoip_ver/geoip-only-cn-private.dat
	v2ray.initd
	v2ray.confd
	"
options="net" # go modules

builddir="$srcdir/$pkgname-core-$pkgver"

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

build() {
	local ldflags="
		-X github.com/v2fly/v2ray-core/v5.codename=$pkgname
		-X github.com/v2fly/v2ray-core/v5.version=$pkgver
		-X github.com/v2fly/v2ray-core/v5.build=$SOURCE_DATE_EPOCH
		-buildid=
		"
	go build -trimpath -ldflags "$ldflags" -o v2ray ./main
}

check() {
	mkdir -p testing/temp
	cp "$srcdir"/geosite-$_geosite_ver.dat testing/temp/geosite.dat
	cp "$srcdir"/geoip-$_geoip_ver.dat testing/temp/geoip.dat

	# network-dependent tests are unreliable
	local pkgs=$(go list ./... | grep -v \
		-e 'v5/testing/scenarios$' \
		-e 'v5/app/dns' \
	)
	go test $pkgs
}

package() {
	install -Dm755 -d "$pkgdir"/etc/$pkgname
	install -m644 release/config/*.json -t "$pkgdir"/etc/$pkgname

	install -Dm755 -d "$pkgdir"/usr/share/$pkgname
	install -m644 "$srcdir"/geosite-$_geosite_ver.dat \
		"$pkgdir"/usr/share/$pkgname/geosite.dat
	install -m644 "$srcdir"/geoip-$_geoip_ver.dat \
		"$pkgdir"/usr/share/$pkgname/geoip.dat
	install -m644 "$srcdir"/geoip-only-cn-private-$_geoip_ver.dat \
		"$pkgdir"/usr/share/$pkgname/geoip-only-cn-private.dat

	install -Dm755 v2ray "$pkgdir"/usr/bin/v2ray

	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -Dm644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
}

sha512sums="
e374d2890977a36b0ff675a38ad92806493c0997d85e2ac3022dfafde4ca9a61e3f91e4d37330841b2f5098b159d473681be9b2a41a4f1f5e9bdba64f3e9942d  v2ray-core-5.21.0.tar.gz
5c8d3e445f7a31317d0658dab7471a74dbbe70fb7d5226e4c96fea3cdc29a8fb1bf52372b73d6ff748b3f01fb65d932752fea71ec11ba25b2aab620559d62088  geosite-20241013063848.dat
5aef93bca11d193d5fc330ff2d608f67222c49d4e0aa8e184fb961f009189fd2a97e5d8ed2df25059e40c40af6df513748724137a828d7f5d7d42475b5601661  geoip-202410240052.dat
3211f78cd02eecd5795020aff1142987d165e83a55d9ea74a2c9722f5f053305f183d65dd382b9f5de0b7c153e2e7a630261b86930e3b11ab23e5e2c70d6f605  geoip-only-cn-private-202410240052.dat
714e9f93efb74a2b30a74df95fa2bdaa854188d5fce3ed4514f2d19e790baf31dc78c18726700013113501dd31981a2e0582150b64d7235121242c01e2e5b8d9  v2ray.initd
4b00bfe001780b9ac7eff1b9776a98ae5e7050362ebfb0ed2550f8f1244e9474d186dcde5bccba01dbdd908e9a932f75238becae6585efe674ebc5ffb8cbf0a6  v2ray.confd
"
